package cj.web.portal.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.kly.domain.GroupPeople;
import cj.web.kly.domain.SpecialistLabel;
import cj.web.login.controllers.BaseController;
import cj.web.portal.service.PeopleService;

/**
 * 学术队伍
 * @author Administrator
 *
 */
@Path("/tream")
public class TreamController extends BaseController  {

	private static Logger logger = Logger.getLogger(TreamController.class.getName());
	
	@Autowired
	private PeopleService peopleService;
	
	//学术队伍
	@Get("academicTeam")
	public String academicTeam(Model model,@Param("lableId") int lableId) throws Exception {
		Map obj = peopleService.queryBySpecialistLabelPid(lableId);
		model.add("obj",obj);
		model.add("lableId",lableId);
		List<Map> repHeadLinklis = returnHeadLink(lableId);
		model.add("repHeadLinklis",repHeadLinklis);
		model.add("columnsMemuKey",repHeadLinklis.get(0).get("value"));
		model.add("columnsMemuCn",repHeadLinklis.get(0).get("name"));
		model.add("columnsMemuEn",repHeadLinklis.get(0).get("en"));
		return "/views/portal/tream/academicTeam.vm";
	}
	
	//人物详情页面-部门其他人员
	@Get("organPersonDetail")
	public String detailPage(Model model,@Param("peopleId") int peopleId) throws Exception {
		Map rep = peopleService.queryBySpecialistLabelDetailPid(peopleId);
		
		List<GroupPeople> groupPeoplelis = (List<GroupPeople>) rep.get("groupPeoplelis");
		int labelId = Integer.parseInt(rep.get("labelId").toString());
		model.add("groupPeoplelis",groupPeoplelis);
		model.add("peopleId",peopleId);
		List<Map> repHeadLinklis = returnDetailLink(labelId);
		model.add("repHeadLinklis",repHeadLinklis);
		return "/views/portal/tream/peopleDetail.vm";
	}
	
	//查询用户详情
	@Post("organPersonDetailFun")
	public String detail(@Param("peopleId") int peopleId) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		GroupPeople people = peopleService.queryById(peopleId);
		jo.put("people",people);
		return "@"+jo.toString();
	}
	
	/**
	 * 
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	private List<Map> returnHeadLink(int labelId) throws Exception{
		List<Map> replis = new ArrayList<Map>();
		String str = getParentName(labelId);
		
		String[] heads = str.split("&");
		for(int i=0;i<heads.length-1;i++){
			Map rep = new HashMap();
			rep.put("name", heads[i].split("@")[0]);
			rep.put("value", heads[i].split("@")[1]);
			rep.put("en", heads[i].split("@").length>2?heads[i].split("@")[2]:"");
			replis.add(rep);
		}
		return replis;
	}
	
	/**
	 * 
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	private List<Map> returnDetailLink(int labelId) throws Exception{
		List<Map> replis = new ArrayList<Map>();
		String str = getParentName(labelId);
		
		String[] heads = str.split("&");
		for(int i=0;i<heads.length;i++){
			Map rep = new HashMap();
			rep.put("name", heads[i].split("@")[0]);
			rep.put("value", heads[i].split("@")[1]);
			rep.put("en", heads[i].split("@").length>2?heads[i].split("@")[2]:"");
			replis.add(rep);
		}
		return replis;
	}
	
	/** 
     * 递归得到当前节点的所有父节点 
     * @param configId 当前节点 
     * @return 所有父节点 
     * @throws Exception 抛出的异常 
     */  
    private String getParentName(int labelId)throws Exception{  
        //和数据库交互,得到当前节点记录  
    	SpecialistLabel columnsEntity = peopleService.querySpecialistLabellis(labelId);
        if(columnsEntity != null){  
            String configName = columnsEntity.getName()+"@"+columnsEntity.getValue()+"@"+columnsEntity.getEn()+"&";  
            //参数patrolConfigEntity.getConfigParentId()表示当前节点的父节点ID  
            String returnConfigName = getParentName(columnsEntity.getpId());  
            return returnConfigName+configName;  
        }else{  
            return "";  
        }  
    }
}